<?php
namespace Common\Controller;
use Think\Controller;

/**
 * Genre Data Model Proxy Class
 *
 * @package Common\Controller
 * @author lythesia
**/
class GenreController extends Controller {

// {{{ shared functions
  /**
   * Get genres data
   *
   * @param  array fields, condition and other options
   * @return array query result
   * @author lythesia
   **/
  public function getGenres($opt) {
    $Genre = M('genreinfo');
    $data = $Genre->field($opt['field'])->where($opt['where'])->limit($opt['limit'])->select();
    return $data;
  }
// }}}


// {{{ inter-module functions
  /**
   * Get genre data by name
   *
   * @param  string genre name
   * @param  string|array fields
   * @return array query result
   * @author lythesia
   **/
  public function getGenreByName($name, $field) {
    $Genre = M('genreinfo');
    if(!is_array($name) && !strpos($name, ',') ) {
      $data = $Genre->field($field)->where(array('name' => $name))->find();
      return $data;
    }
    $data = $Genre->field($field)->where(array('name' => array('in', $name)))->select();
    return $data;
  }

  /**
   * Get top & recent movies(with relations and paging support)
   *
   * @param  conditons and options
   * @return query result
   * @author lythesia
   **/
  public function getTopNewMovies($opt) {
    $Genre = D('Genre');
    $data = $Genre->field($opt['genre_field'])->where($opt['genre_cond'])->find();
    // prepare options
    $_limit = (((int)$opt['page']-1)*$opt['count']).','.$opt['count'];

    $data['movies'] = $Genre->setRelField('Movie', $opt['movie_field'])->setRelOrder('Movie', $opt['movie_order'])->setRelLimit('Movie', $_limit)->relationGet('movies');
    return $data;
  }
// }}}
}
